import datetime
from SystematicFactors.General import *
import os
import Core.Gadget as Gadget


def Calc_Industrial_Added(database, datetime1, datetime2):

    df15 = Query_Data(database,'M0000545',datetime1,datetime2)  # 工业增加值:当月同比
    df15['factor'] = df15.diff()
    # df15['Report_Date'] = df15.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df15['Report_Date'] = df15.index
    Fill_ReleaseDate(df15, lag_release_month=1, release_day=15)
    print(df15)
    #
    Save_Systematic_Factor_To_Database(database, df15, save_name='Industrial_Value_Added_YoY', field_name='M0000545')
    #
    df15.dropna(subset=["factor"], inplace=True)
    Save_Systematic_Factor_To_Database(database, df15, save_name='Industrial_Value_Added_YoY_Dif', field_name='factor')
    return(df15,'Industrial_Value_Added_YoY')


def Calc_RealEstate(database, datetime1, datetime2):
    #
    df16 = Query_Data(database, 'S0029657', datetime1, datetime2)  # 房地产开发投资完成额:累计同比
    df16['factor'] = df16.diff()
    # df16['Report_Date'] = df16.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df16['Report_Date'] = df16.index
    Fill_ReleaseDate(df16, lag_release_month=1, release_day=18)
    print(df16)
    Save_Systematic_Factor_To_Database(database, df16, save_name='RE_Invested_YoY', field_name='S0029657')
    df16.dropna(subset=["factor"], inplace=True)
    Save_Systematic_Factor_To_Database(database, df16, save_name='RE_Invested_YoY_Dif', field_name='factor')
    return(df16,'RE_Invested_YoY_Dif')


# accfixinvs
def Calc_Fixed_Asset_Invested(database, datetime1, datetime2):
    #
    df17 = Query_Data(database,'M0000273',datetime1,datetime2)  # 固定资产投资完成额:累计同比
    df17["date"] = pd.to_datetime(df17.index)
    real_datetime1 = df17.iloc[0]["date"]
    real_datetime2 = df17.iloc[-1]["date"]
    print(df17)

    # 一月份不公布数据，需要对齐
    df = Gadget.Generate_Calender_Days_DataFrame(real_datetime1, real_datetime2, date_field_name="date")
    df = pd.merge(df, df17, how="left", on="date")
    df.index = df["date"]
    df.fillna(method="ffill", inplace=True)
    #
    df_monthly = df.resample("M").last()
    df_monthly["Report_Date"] = df_monthly.index
    Fill_ReleaseDate(df_monthly, lag_release_month=1, release_day=15)
    #
    df_monthly['factor'] = df_monthly["M0000273"].diff()
    print(df_monthly)
    #
    df_monthly.dropna(subset=["M0000273"], inplace=True)
    Save_Systematic_Factor_To_Database(database, df_monthly, save_name='Fixed_Asset_Invested_Aggr_YoY',
                                       field_name='M0000273')
    #
    df_monthly.dropna(subset=["factor"], inplace=True)
    Save_Systematic_Factor_To_Database(database, df_monthly, save_name='Fixed_Asset_Invested_Aggr_YoY_Dif',
                                       field_name='factor')
    return(df17,'Fixed_Asset_Invested_Aggr_YoY')


# exim
def Calc_Net_Export_Ratio(database, datetime1, datetime2):
    #
    df1 = Query_Data(database, 'M0000610',datetime1,datetime2)  # 贸易差额:当月值
    df2 = Query_Data(database, 'M0000604',datetime1,datetime2)  # 进出口金额:当月值
    df20 = pd.merge(df1, df2, how="inner", left_index=True, right_index=True)
    df20['factor'] = df20['M0000610'] / df20['M0000604']
    # df20['Report_Date'] = df20.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df20['Report_Date'] = df20.index
    Fill_ReleaseDate(df20, lag_release_month=1, release_day=23)
    print(df20)
    #return(df20,'Net_Export_Ratio')
    Save_Systematic_Factor_To_Database(database, df20, save_name='Net_Export_Ratio', field_name='factor')


#
def Calc_Price_Level(database, datetime1, datetime2):
    #
    df_cpi = EDB('M0000612', datetime1, datetime2, 'CPI_YoY', dateAsIndex=False)  # CPI:当月同比
    Fill_ReleaseDate(df_cpi, lag_release_month=1, release_day=10)
    # 储存原始数据
    Save_Systematic_Raw_To_Database(database, df_cpi, saved_name="M0000612", field_name="CPI_YoY")
    # 储存为因子数据
    Save_Systematic_Factor_To_Database(database, df_cpi, save_name='CPI_YoY')

    #
    df1 = Query_Data(database,'M0000613',datetime1,datetime2)  # CPI:非食品:当月同比
    df2 = Query_Data(database,'M0001227',datetime1,datetime2)  # PPI:全部工业品:当月同比
    df18 = pd.merge(df1, df2, how="inner", left_index=True, right_index=True)
    df18['factor'] = df18['M0000613'] - df18['M0001227']
    # df18['Report_Date'] = df18.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df18['Report_Date'] = df18.index
    Fill_ReleaseDate(df18, lag_release_month=1, release_day=12)
    # print(df18)
    # return(df18,'CPI_PPI_Spread_NoFood')
    Save_Systematic_Factor_To_Database(database, df18, save_name='CPI_PPI_Spread_NoFood', field_name='factor')




# 企业负债率变动
# dif_liability
def Calc_Industrial_Liability_Ratio(database, datetime1, datetime2):
    #
    df_ratio = EDB('M0044700', datetime1, datetime2)  # 工业企业资产负债率
    df_liab = EDB('M0044695', datetime1, datetime2)  # 工业企业：负债合计
    df_asset = EDB('M0007501', datetime1, datetime2)  # 工业企业：资产合计
    #
    df = pd.concat([df_ratio, df_liab, df_asset], axis=1, sort=True)

    df['naive_ratio'] = df["M0044700"]/100
    df['ratio'] = df['M0044695'] / df['M0007501']

    df['Report_Date'] = df.index
    Fill_ReleaseDate(df, lag_release_month=1, release_day=28)  # 真的是27-28号左右才公布
    #
    df["ratio_dif"] = df["ratio"].diff(1)
    df["naive_ratio_dif"] = df["naive_ratio"].diff(1)
    #
    # print(df)
    # df.to_csv("d://Calc_Industrial_Liability_Ratio.csv")
    #
    Save_Systematic_Factor_To_Database(database, df, save_name='Industrial_Liab_Ratio_Native', field_name='naive_ratio')
    Save_Systematic_Factor_To_Database(database, df, save_name='Industrial_Liab_Ratio_Native_Dif',
                                       field_name='naive_ratio_dif')
    Save_Systematic_Factor_To_Database(database, df, save_name='Industrial_Liab_Ratio', field_name='ratio')
    Save_Systematic_Factor_To_Database(database, df, save_name='Industrial_Liab_Ratio_Dif', field_name='ratio_dif')



# dif_profmargin
def Calc_Industrial_Profmargin(database, datetime1, datetime2):
    #
    df22 = Query_Data(database,'M5207655',datetime1,datetime2)  # 工业企业:营业收入利润率:累计值
    df22['factor'] = df22.diff()/100
    # df22['Report_Date'] = df22.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df22['Report_Date'] = df22.index
    Fill_ReleaseDate(df22, lag_release_month=1, release_day=28)
    print(df22)
    df22.dropna(subset=["factor"], inplace=True)
    # return(df22,'Industrial_Profitmargin_Dif')
    Save_Systematic_Factor_To_Database(database, df22, save_name='Industrial_Profitmargin_Dif', field_name='factor')


# dif_att
def Calc_Industrial_ATO(database, datetime1, datetime2):
    #
    df1 = Query_Data(database,'M0000554',datetime1,datetime2)  # 工业企业:主营业务收入:累计值
    df2 = Query_Data(database,'M0007501',datetime1,datetime2)  # 工业企业:资产合计
    df23 = pd.merge(df1, df2, how="inner", left_index=True, right_index=True)
    df23['M0000554'] = df23['M0000554'].diff()
    df23['M0000554'].fillna(method='ffill',inplace=True)
    df23['div'] = df23['M0007501'] / df23['M0000554']
    df23['factor'] = df23['div'].diff()
    # df23['Report_Date'] = df23.index.astype(str).map(lambda x: pd.datetime.strptime(x, '%Y-%m-%d'))
    df23['Report_Date'] = df23.index
    df23['Release_Date'] = df23['Report_Date']
    #
    print(df23[["Report_Date", "Release_Date", "div", "factor"]])
    #return(df23,'Industrial_ATO_Dif')
    Save_Systematic_Factor_To_Database(database, df23, save_name='Industrial_ATO_Dif', field_name='factor')


def Calc_Consumption(database, datetime1, datetime2):
    #
    datetime_early = datetime1 + datetime.timedelta(days=-90)
    df = EDB('M0001428', datetime_early, datetime2)  # 社会零售品销售总额 同比

    # 每年1 2月数据一起更新 因此1月末数据缺失 填前值
    df = Fix_Missing_Monthly(df)

    # 每月14-15日更新上一月数据 这里用15
    df["Report_Date"] = df.index
    Fill_ReleaseDate(df, lag_release_month=1, release_day=15)
    df["Retail"] = df["M0001428"]
    df["Retail_Dif"] = df["M0001428"].diff(1)
    # print(df[["Release_Date", "Retail", "Retail_Dif"]])
    Save_Systematic_Factor_To_Database(database, df, save_name='Retail_Sales_YoY', field_name="Retail")
    Save_Systematic_Factor_To_Database(database, df, save_name='Retail_Sales_YoY_Monthly_Dif', field_name="Retail_Dif")


def Calc_PMI(database, datetime1, datetime2):

    # 预测平均值:PMI（可能已经改名，万得一致预测:中国:PMI），M0331594 代码可能写错，需要重新刷新
    df_pmi_estimate = EDB('M0331594', datetime1, datetime2, 'Estimate_Avg_PMI')
    Save_Systematic_Factor_To_Database(database, df_pmi_estimate, save_name='Estimate_Avg_PMI')

    # PMI 非制造业：商务活动
    df_pmi_business = EDB('M0048236', datetime1, datetime2, 'PMI_Business')
    df_pmi_business["Report_Date"] = df_pmi_business.index
    Fill_ReleaseDate(df_pmi_business, lag_release_month=1, release_day=1)
    Save_Systematic_Factor_To_Database(database, df_pmi_business, save_name='PMI_Business')

    # PMI 非制造业：服务业
    df_pmi_services = EDB('M5207838', datetime1, datetime2, 'PMI_Services')
    df_pmi_services["Report_Date"] = df_pmi_services.index
    Fill_ReleaseDate(df_pmi_services, lag_release_month=1, release_day=1)
    Save_Systematic_Factor_To_Database(database, df_pmi_services, save_name='PMI_Services')

    # 全球:摩根大通全球制造业PMI
    df_jpm_pmi = EDB('G8400010', datetime1, datetime2, 'JP_Morgan_PMI')
    df_jpm_pmi["Report_Date"] = df_jpm_pmi.index
    Fill_ReleaseDate(df_jpm_pmi, lag_release_month=1, release_day=2)
    Save_Systematic_Factor_To_Database(database, df_jpm_pmi, save_name='JP_Morgan_PMI')

    # PMI
    df_pmi = EDB('M0017126', datetime1, datetime2, 'PMI')
    df_pmi["Report_Date"] = df_pmi.index
    Fill_ReleaseDate(df_pmi, lag_release_month=1, release_day=1)
    Save_Systematic_Factor_To_Database(database, df_pmi, save_name='PMI')

    # PMI:生产
    df_pmi_produce = EDB('M0017127', datetime1, datetime2, 'PMI_Produce')
    df_pmi_produce["Report_Date"] = df_pmi_produce.index
    Fill_ReleaseDate(df_pmi_produce, lag_release_month=1, release_day=1)
    Save_Systematic_Factor_To_Database(database, df_pmi_produce, save_name='PMI_Produce')

    # PMI:新订单
    df_pmi_newOrder = EDB('M0017128', datetime1, datetime2, 'PMI_NewOrder')
    df_pmi_newOrder["Report_Date"] = df_pmi_newOrder.index
    Fill_ReleaseDate(df_pmi_newOrder, lag_release_month=1, release_day=1)
    Save_Systematic_Factor_To_Database(database, df_pmi_newOrder, save_name='PMI_NewOrder')

    # 财新中国PMI
    df_caixin_pmi = EDB('M0000138', datetime1, datetime2, 'CaiXin_PMI')
    df_caixin_pmi["Report_Date"] = df_caixin_pmi.index
    Fill_ReleaseDate(df_caixin_pmi, lag_release_month=1, release_day=1)
    Save_Systematic_Factor_To_Database(database, df_caixin_pmi, save_name='CaiXin_PMI')


def download_bci_indicator(database, datetime1, datetime2):
    # 参考 Systematic Factors
    # BCI:企业利润前瞻指数
    df_1 = EDB('M5786900', datetime1, datetime2, 'BCI_Earning_Indicator')
    Save_Systematic_Factor_To_Database(database, df_1, save_name='BCI_Earning_Indicator')


if __name__ == '__main__':
    #
    path_filename = os.getcwd() + "\..\Config\config_local.json"
    database = Config.create_database(database_type="MySQL", config_file=path_filename, config_field="MySQL")

    w.start()

    datetime1 = datetime.datetime(2023, 11, 1)
    datetime2 = datetime.datetime(2024, 5, 1)
    # Calc_PMI(database, datetime1, datetime2)
    # Calc_Social_Financing(database, datetime1, datetime2)
    # download_bci_indicator(database, datetime1, datetime2)

    datetime1 = datetime.datetime(2000, 1, 1)
    datetime2 = datetime.datetime(2024, 5, 26)
    # Calc_Consumption(database, datetime1, datetime2)
    # Calc_Industrial_Liability_Ratio(database, datetime1, datetime2)
